//
//  XMHomeVC.m
//  XMWebTool
//
//  Created by ext.zhangmingwei8 on 2023/12/7.
//

#import "XMHomeVC.h"
#import "XMCustomNaviView.h"
#import "XMFavoriteListVC.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIColor+XMTool.h"
#import "XMWebView.h"
#import "XMSizeMacro.h"
#import "UIView+XMFrame.h"
#import "XMWebViewManager.h"
#import "XMSettingVC.h"
// 下拉刷新
#import "UIScrollView+MJRefresh.h"
#import "MJRefreshNormalHeader.h"

@interface XMHomeVC ()<WKNavigationDelegate>

@property (nonatomic, strong) UIScrollView      *scrollView;
@property (nonatomic, strong) XMCustomNaviView  *naviView;

/// 是否有加载成功的
@property (nonatomic, assign) BOOL  isFinishFlag;

@end

@implementation XMHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getWhiteColor_1];
    self.fd_prefersNavigationBarHidden = YES;
    self.naviView = [XMCustomNaviView getInstanceWithTitle:@"网页工具"];
    [self.view addSubview:self.naviView];
    [self.naviView setRightBtnImage:nil title:@"收藏列表"];
    [self.naviView setBackBtnImage:[UIImage imageNamed:@"icon_more"] title:nil];
    __weak typeof(self) weakSelf = self;
    [self.naviView setRightBlock:^{
        XMFavoriteListVC *vc = [XMFavoriteListVC new];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self.naviView setBackBlock:^{
        XMSettingVC *vc = [XMSettingVC new];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [self initAllView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showBigAction) name:@"kNotifi_showBig" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSmallAction) name:@"kNotifi_showSmall" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadPage) name:@"kNotifi_reloadPage" object:nil];

    [self showSmallAction];
}

- (void)initAllView {
    [self.view addSubview:self.scrollView];
    NSMutableArray *urlArr = [XMWebViewManager defalutManager].urlArr;
    
    CGFloat space = 4;
    CGFloat webWidth = (kScreenWidth_XM - space*3.0)/2.0;
    CGFloat webHeight = webWidth * 1.6;
    CGFloat xScale = webWidth/kScreenWidth_XM;
    CGFloat yScale = webHeight/(kScreenHeight_XM - kStatusBarHeight_XM);
    
    CGFloat webBottom = 0.0;
    for (int i = 0; i < urlArr.count; i++) {
        NSString *urlString = urlArr[i];
        XMWebView *webV = [[XMWebView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight_XM, kScreenWidth_XM, kScreenHeight_XM - kStatusBarHeight_XM)];
        [webV addUrlString:urlString];
        webV.webView.navigationDelegate = self;
        [self.scrollView addSubview:webV];
        [webV setScaleWithX:xScale yScale:yScale];
        [webV showSmallAction];
        CGFloat x = space + (i % 2) * (webV.width + space);
        CGFloat y = space + (i / 2) * (webV.height + space - 130) + 44;
        webV.frame = CGRectMake(x, y, webV.width, webV.height);
        webBottom = webV.bottom;
    }
    
    self.scrollView.contentSize = CGSizeMake(kScreenWidth_XM, webBottom);
    
    // 添加下拉刷新
    __weak typeof(self) wSelf = self;
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [wSelf onlyReloadPage];
        [wSelf.scrollView.mj_header endRefreshing];
        if (wSelf.isFinishFlag == NO) { // 都不成功，说明是刚才没开网络之类的。
            [wSelf reloadPage];
        }
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"加载网页OK了。");
    self.isFinishFlag = YES;
}

- (void)reloadPage {
    // 刷新缓存数据
    [XMWebViewManager reloadLocalUrlArrayAction];
    for (UIView *v in self.scrollView.subviews) {
        [v removeFromSuperview];
    }
    [self.scrollView removeFromSuperview];
    self.scrollView = nil;
    [self initAllView];
}

- (void)onlyReloadPage {
    for (UIView *v in self.scrollView.subviews) {
        if ([v isKindOfClass:[XMWebView class]]) {
            [((XMWebView *)v).webView reload];
        };
    }
}

- (void)showBigAction {
    [self.view bringSubviewToFront:self.scrollView];
}

- (void)showSmallAction {
    [self.view bringSubviewToFront:self.naviView];
    
    NSArray *localArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUrlArr"];
    for (int i = 0; i < localArr.count; i++) {
        NSString *urlS = localArr[i];
        XMWebView *targtV = [self getXMWebView:urlS];
        if (targtV) {
            [self.scrollView bringSubviewToFront:targtV];
        }
    }
}

/// 查找某一个网页view
- (XMWebView *)getXMWebView:(NSString *)urlString {
    for (UIView *v in self.scrollView.subviews) {
        if ([v isKindOfClass:[XMWebView class]]) {
            NSString *urlS = ((XMWebView *)v).urlString;
            if ([urlS isEqualToString:urlString]) {
                return (XMWebView *)v;
            }
        }
    }
    return nil;
}


- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight_XM, kScreenWidth_XM, kScreenHeight_XM - kStatusBarHeight_XM)];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

@end
